using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;
using System.Xml;

namespace TelephoneDirectory
{
    class Search
    {

        public void SearchByName(string myName)
        {
            int i = 0,j=0;
            XmlDocument myDoc = new XmlDocument();
            myDoc.Load("telephone.xml");

            XmlNodeList nodeList = myDoc.GetElementsByTagName("Name");

            foreach (XmlNode node in nodeList)
            {
                string title = node.ChildNodes[0].Value;
                
                if (title.Equals(myName))
                {
                    XmlNode nodeNumber = myDoc.GetElementsByTagName("PhoneNumber")[i];
                    Console.WriteLine("\t{0} : {1}  {2} ",j, string.Format("{0,-20}",myName),nodeNumber.ChildNodes[0].Value);
                    j++;
                }
                i++;
            }
            if(j==0)
                Console.WriteLine("\nTen thue bao khong ton tai ");
            Console.WriteLine();
        }
        //--------------------------------------------------------------------------
        public void SearchByNumer(string myNumber)
        {
            int i=0,j=0;
            XmlDocument myDoc = new XmlDocument();
            myDoc.Load("telephone.xml");

            XmlNodeList nodeList = myDoc.GetElementsByTagName("PhoneNumber");
            foreach (XmlNode node in nodeList)
            {
                string phone = node.ChildNodes[0].Value;
                if(phone.Equals(myNumber))
                {
                    XmlNode nodeName = myDoc.GetElementsByTagName("Name")[i];
                    Console.WriteLine("\t{0} : {1}  {2} ", j,string.Format("{0,-20}", nodeName.ChildNodes[0].Value), myNumber);
                    j++;
                }
                i++;
            }
            if (j == 0)
                Console.WriteLine("\nSo thue bao khong ton tai ");
            Console.WriteLine();
        }
        //---------------------------------------------------------------------
        public void ViewAll()
        {
            int i=0;
            XmlDocument myDoc = new XmlDocument();
            myDoc.Load("telephone.xml");

            XmlNodeList nodeList = myDoc.GetElementsByTagName("Name");
            foreach (XmlNode node in nodeList)
            {
                XmlNode nodeNumber = myDoc.GetElementsByTagName("PhoneNumber")[i];
                Console.WriteLine("\t{0} : {1}   {2} ", i,string.Format("{0,-20}", node.ChildNodes[0].Value), nodeNumber.ChildNodes[0].Value);
                i++;
            }
            if (i == 0)
                Console.WriteLine("Danh ba rong ");
            Console.WriteLine();
        }
    }
}
